<template>
  <div class="playlist-song">
    <div class="head">
      <div class="choose">
        <div @click="changeColor(1)" :class="{ choosed: Name == 'list' }">
          播放列表
        </div>
        <div @click="changeColor(2)" :class="{ choosed: Name == 'history' }">
          历史记录
        </div>
      </div>
      <div class="close" @click="show">×</div>
    </div>
    <div class="playlist" v-if="Name == 'list'">
      <div class="song-title">
        <span class="sum">总{{ playlist.length }}首</span>
        <span class="clear song-left" @click="del({ type: 'all' })">
          <img src="../assets/qk.png" alt />
          清空
        </span>
        <span class="line"></span>
        <span class="collect song-left">
          <img src="../assets/collect.png" alt />
          收藏全部
        </span>
      </div>
      <ul class="song-lists">
        <li
          class="playing"
          :class="{ bgc: index == current }"
          v-for="(item, index) in playlist"
          :key="item.id"
          @click="play(index, item.id, item.songName, item.name)"
        >
          <div :class="{ play: index == current }"></div>
          <span class="songName">{{ item.songName }}</span>
          <div class="lefts">
            <span class="singer">{{ item.name }}</span>
            <div>
              <span>{{ item.time }}</span>
              <span
                class="delete"
                @click.stop="del({ type: 'one', id: item.id })"
                >×</span
              >
            </div>
          </div>
        </li>
      </ul>
    </div>
    <div class="history playlist" v-if="Name == 'history'">
      <div class="song-title">
        <span class="sum">总{{ history.length }}首</span>
        <span class="clear song-left" @click="delHis()">
          <img src="../assets/qk.png" alt />
          清空
        </span>
      </div>
      <ul class="song-lists">
        <li
          class="playing"
          :class="{ bgc: index == current }"
          v-for="(item, index) in history"
          :key="item.id"
          @click="play(index, item.id)"
        >
          <span class="songNames">{{ item.songName }}</span>
          <div class="lefts">
            <span class="singer">{{ item.Name }}</span>
            <div>
              <span>{{ item.time | backTime }}</span>
              <span class="delete" @click.stop="delHis(item.id)">×</span>
            </div>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";

export default {
  data() {
    return {
      Name: "list", //选项卡
      current: 0, //当前点击元素
    };
  },
  computed: {
    ...mapState(["playlist", "Nowplay", "history"]), //播放列表
  },
  filters: {
    backTime(time) {
      var timestamp = new Date().getTime();
      let m = parseInt((timestamp - time) / 1000 / 60); //分
      if (m < 1) {
        return "刚刚";
      } else {
        if (m / 60 >= 24) {
          //大于一天
          let date = new Date(time);
          let year = date.getFullYear();
          let month = date.getMonth() + 1;
          let day = date.getDate();
          return year + "-" + month + "-" + day;
        } else if (m / 60 < 24 && m / 60 > 1) {
          return parseInt(m / 60) + "小时前";
        } else {
          return m + "分钟前";
        }
      }
    },
  },
  methods: {
    changeColor(n) {
      switch (n) {
        case 1:
          this.Name = "list";
          break;
        case 2:
          this.Name = "history";
          break;
        default:
          this.Name = "list";
          break;
      }
    },
    show() {
      this.$emit("show");
    },
    del(obj) {
      this.$store.commit("delPlayList", obj);
    },
    delHis(id) {
      this.$store.commit("delHis", id);
    },
    play(index, id, songName, name) {
      this.current = index;
      let list = { id };
      if (songName) {
        list = { id, songName, name };
      }

      this.$store.commit("plushplay", list);
    },
  },
  mounted() {
    let current = this.playlist.findIndex((item) => item.id == this.Nowplay);
    this.current = current;
  },
  watch: {
    Nowplay(val) {
      if (val) {
        let current = this.playlist.findIndex(
          (item) => item.id == this.Nowplay
        );
        this.current = current;
      }
    },
  },
};
</script>
<style lang="less" scoped>
.playlist-song {
  width: inherit;
  height: inherit;
  box-shadow: -1px -1px 2px 1px rgb(194, 194, 195);
  background-color: white;
  .head {
    height: 40px;
    background-color: rgb(244, 244, 246);
    border-bottom: 1px solid rgb(225, 225, 226);
    text-align: center;
    padding: 5px 10px;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    .choose {
      width: 220px;
      height: 25px;
      text-align: center;
      font-size: 12px;
      line-height: 25px;
      border-radius: 5px;
      overflow: hidden;
      display: flex;
      justify-content: space-between;
      div {
        flex: 1;
        color: #888;
        border: 1px solid rgb(225, 225, 226);
      }
      .choosed {
        background-color: #7c7d85;
        color: #fff;
        border: none;
      }
    }
    .close {
      position: absolute;
      right: 0;
      top: 0;
      font-size: 20px;
      margin: 5px 10px;
      cursor: pointer;
    }
  }
  .playlist {
    height: ceil(100%);
    .song-title {
      height: 30px;
      width: 100%;
      line-height: 30px;
      text-align: left;
      padding: 0 30px;
      border-bottom: 1px solid #e1e1e2;
      color: #666;
      font-size: 12px;
      .song-left {
        float: right;
        display: flex;
        justify-content: center;
        align-items: center;
        img {
          width: 12px;
          height: 12px;
          margin-right: 5px;
        }
      }
      .line {
        display: inline-block; 
        height: 14px;
        width: 1px;
        float: right; 
        margin: 8px 5px;
        background-color: #ccc;
      }
    }
    .song-lists {
      list-style-type: none;
      width: 100%;
      height: calc(100% - 70px);
      overflow-y: auto;
      & li:nth-child(2n) {
        background-color: #f5f5f7;
      }
      .playing {
        width: 100%;
        height: 25px;
        line-height: 25px;
        padding-left: 30px;
        text-align: left;
        display: flex;
        justify-content: space-between;
        position: relative;
        .play {
          position: absolute;
          width: 0;
          height: 0;
          border: 5px solid transparent;
          border-left: 7px solid #c62f2f;
          top: 7px;
          left: 10px;
        }
        &:hover .lefts .delete {
          display: inline-block;
          color: black;
        }
        &:hover {
          background-color: #ecedee;
        }
        &:hover span:not(.songName) {
          color: black;
        }
        span {
          display: inline-block;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .songName {
          flex: 3.5;
          font-size: 11px;
        }
        .songNames {
          flex: 2.5;
          font-size: 11px;
        }
        .lefts {
          flex: 1;
          display: flex;
          justify-content: space-between;
          color: #888;
          font-size: 10px;
          span,
          div {
            flex: 1;
          }
          .delete {
            display: none;
            font-size: 12px;
            margin-left: 10px;
          }
        }
      }
      .bgc {
        background-color: #e3e3e5 !important;
      }
    }
  }
}
</style>
